

package infb.servlet.movil;

import infb.dao.MedicamentosInterfaz;
import infb.modelo.Medicamentos;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class MedicamentosServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, NamingException {
        System.out.println("Medicamentoooooooooooooooooo");
        Connection con = null;

    DataOutputStream dataOut = new DataOutputStream(response.getOutputStream());
    DataInputStream dis = new DataInputStream(request.getInputStream());
    Context contexto = new InitialContext();
    MedicamentosInterfaz medicamentoInterfaz = (MedicamentosInterfaz) contexto.lookup("MedicamentosEJB");
    List<Medicamentos> listaMedicamentos=null;
    listaMedicamentos=medicamentoInterfaz.findAll();
    String medicamento=dis.readUTF();
    System.out.println("Medicamento"+medicamento);
    if (medicamento.equals("medicamento")){
    EnviarMedicamentos(listaMedicamentos,dataOut);}
    else{
    System.out.println("Algo estA MALL");
    }


    dataOut.flush();
    dataOut.close();

    }
    private void EnviarMedicamentos(List<Medicamentos> listaMed,DataOutputStream dataOut) throws IOException{
System.out.println("Has entrado al procedimiento para enviar al moviiil");
    int longitud=listaMed.size();
    String nombre,definicion;
    Medicamentos medicamento;
    dataOut.write(longitud);
    for (int i=0; i<longitud; i++) {
        medicamento = listaMed.get(i);
        nombre=medicamento.getNombre();
        definicion=medicamento.getDefinicion();
        System.out.println("nombre"+nombre);
        System.out.println("Definicion"+definicion);
        dataOut.writeUTF(nombre);
        dataOut.writeUTF(definicion);
    }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (NamingException ex) {
            Logger.getLogger(MedicamentosServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (NamingException ex) {
            Logger.getLogger(MedicamentosServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
